package com.yzt.base.utils;

import org.apache.commons.lang3.StringUtils;

import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;


/**
 * @author Y
 * @date 2020-12-03
 */
public class RequestUtil {
    /**
     * 获取真实IP地址
     */
    public static String getRemoteHost(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        System.out.println("请求来源IP:" + ip);
        return ip;
    }

    public static String getPlatForm(HttpServletRequest request) {
        String agent = request.getHeader("User-Agent");
        if (agent == null) {
            return "(Unknown Device)";
        }
        try {
            agent = agent.substring(agent.indexOf("("), agent.length());
        } catch (Exception e) {
            return agent;
        }
        return agent;
    }

    public static boolean isAjax(HttpServletRequest request) {
        String header = request.getHeader("x-requested-with");
        System.out.println(header);
        if (StringUtils.isBlank(header)) {
            return false;
        }
        return true;
    }

    @SuppressWarnings("rawtypes")
    public static Map<String, Object> getParameterMap(HttpServletRequest request) {
        Map<String, Object> params = new HashMap<String, Object>();
        Map requestParams = request.getParameterMap();
        for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext(); ) {
            String name = (String) iter.next();
            if (name.equals("page"))
                continue;
            if (name.equals("limit"))
                continue;
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
            }
            // valueStr = new String(valueStr.getBytes("ISO-8859-1"),"gbk");
            try {
                valueStr = URLDecoder.decode(valueStr, "UTF-8");
                params.put(name, valueStr);
            } catch (Exception e) {
                params.put(name, valueStr);
            }
        }
        return params;
    }

    public static void setParameterMap(HttpServletRequest request, Map<String, Object> params) {
        if (params == null)
            return;
        Iterator<Map.Entry<String, Object>> iterator = params.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Object> next = iterator.next();
            request.setAttribute(next.getKey(), next.getValue());
        }
    }
}
